package org.pro.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.social.security.SocialUser;
import org.springframework.social.security.SocialUserDetails;
import org.springframework.social.security.SocialUserDetailsService;
import org.springframework.stereotype.Component;

@Component
//此处需要判断不存在userService的情况
public class MyUserDetailsService implements UserDetailsService,SocialUserDetailsService {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	//注入加密器
	@Autowired
	private PasswordEncoder passwordEncoder;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		logger.info("登录用户名"+username);
		//此处写自己的user数据来源，然后封装即可。
		//AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN") 是角色赋值
		return new User(username,passwordEncoder.encode("123456"),AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
	}

	@Override
	public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException {
		return new SocialUser(userId,passwordEncoder.encode("123456"),AuthorityUtils.commaSeparatedStringToAuthorityList("ADMIN"));
	}
}
